package cn.msn.product.service.impl;

import cn.msn.basic.service.IBaiduAiAuditService;
import cn.msn.basic.service.impl.BaseServiceImpl;
import cn.msn.basic.util.AjaxResult;
import cn.msn.basic.util.LoginContext;
import cn.msn.org.domain.Employee;
import cn.msn.org.mapper.EmployeeMapper;
import cn.msn.product.domain.Product;
import cn.msn.product.domain.ProductDetail;
import cn.msn.product.domain.ProductOnlineAuditLog;
import cn.msn.product.mapper.ProductDetailMapper;
import cn.msn.product.mapper.ProductMapper;
import cn.msn.product.mapper.ProductOnlineAuditLogMapper;
import cn.msn.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Override
    @Transactional
    public void save(Product product) {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    @Transactional
    public void remove(Long id) {
        super.remove(id);
        //删除服务详情
        productDetailMapper.removeByProductId(id);
    }

    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        //修改服务详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }
    }

    @Override
    public void patchRemove(List<Long> ids) {
        super.patchRemove(ids);
        //删除服务详情
        productDetailMapper.patchRemoveByProductId(ids);
    }

    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        //1.对每一个服务都要审核：审核name和resources["",多个数据]
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            //审核文本
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            //审核图片
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());

            //公共的信息：
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(id);
            //获取登陆信息id
            Long logininfoId = LoginContext.getLogininfo(request).getId();
            Employee employee = employeeMapper.loadByLogininfoId(logininfoId);
            log.setAuditId(employee.getId());

            if(boo1 && boo2){//文本和图片都审核成功
                //2.审核通过：改状态和时间 + 记录审核日志，状态改为1 - 上架
                productMapper.onsale(new Date(),id);

                log.setState(1);//1表示审核成功
                log.setNote("审核成功");//审核日志
                productOnlineAuditLogMapper.save(log);
            }else{
                //3.审核失败：记录审核日志
                log.setState(0);//0表示审核失败
                log.setNote("审核失败，图片和文本非法!!!");//审核日志
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败：图片，文本非法，或图片不存在");
            }

        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        // 操作数据库：改下架时间为当前系统时间
        productMapper.offsale(new Date(),ids);
    }
}
